package com.yanqu.road.dao.impl.activity.snowgarden;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.snowgarden.SnowGardenSkinData;
import com.yanqu.road.entity.activity.snowgarden.SnowGardenUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SnowGardenUserDataDaoImpl extends TempDataObjectDao<SnowGardenUserData> {

    public static String getSnowGardenUserDataAllSql = "select * from `t_u_activity_snow_garden_user` where `activityId`=?";

    public Map<Long, SnowGardenUserData> getSnowGardenUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SnowGardenUserData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSnowGardenUserDataAllSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SnowGardenUserData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
    public static String getSnowGardenUserDataSql = "select * from `t_u_activity_snow_garden_user` where `activityId`=? and `userId`=?";

    public SnowGardenUserData getSnowGardenUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSnowGardenUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSnowGardenUserDataSql = "update `t_u_activity_snow_garden_user` set period=?,loopNum=?,indexNum=?,resultList=?,indexData=?,suit=?,score=?,secondScore=?,skinList=?,invitedIdList=?,combList=?,isRecharge=?,guarantee=?,dice=?,invitedNum=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SnowGardenUserData snowGardenUserData) {
        snowGardenUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getPeriod()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getLoopNum()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getIndexNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getResultList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getIndexData())));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getSuit()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getSecondScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getSkinList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getInvitedIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getCombList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, snowGardenUserData.isRecharge()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getGuarantee()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getDice())));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getInvitedNum()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, snowGardenUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSnowGardenUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSnowGardenUserDataSql = "insert into `t_u_activity_snow_garden_user`(activityId,userId,period,loopNum,indexNum,resultList,indexData,suit,score,secondScore,skinList,invitedIdList,combList,isRecharge,guarantee,dice,invitedNum) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SnowGardenUserData snowGardenUserData) {
        snowGardenUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, snowGardenUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getPeriod()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getLoopNum()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getIndexNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getResultList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getIndexData())));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getSuit()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getSecondScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getSkinList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getInvitedIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getCombList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, snowGardenUserData.isRecharge()));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getGuarantee()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(snowGardenUserData.getDice())));
        para.put(i++, new DbParameter(Types.INTEGER, snowGardenUserData.getInvitedNum()));
        if (getDb().executeNoneQuery(insertSnowGardenUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SnowGardenUserData parseData(ResultSet rs) throws SQLException {
        SnowGardenUserData snowGardenUserData = new SnowGardenUserData();
        snowGardenUserData.setActivityId(rs.getInt("activityId"));
        snowGardenUserData.setUserId(rs.getLong("userId"));
        snowGardenUserData.setPeriod(rs.getInt("period"));
        snowGardenUserData.setLoopNum(rs.getInt("loopNum"));
        snowGardenUserData.setIndexNum(rs.getInt("indexNum"));
        snowGardenUserData.setResultList(JSON.parseObject(rs.getString("resultList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        snowGardenUserData.setIndexData(JSON.parseObject(rs.getString("indexData"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.snowgarden.SnowGardenMapData>>() {}));
        snowGardenUserData.setSuit(rs.getInt("suit"));
        snowGardenUserData.setScore(rs.getInt("score"));
        snowGardenUserData.setSecondScore(rs.getInt("secondScore"));
        snowGardenUserData.setSkinList(JSON.parseObject(rs.getString("skinList"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.snowgarden.SnowGardenSkinData>>() {}));
        snowGardenUserData.setInvitedIdList(JSON.parseObject(rs.getString("invitedIdList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        snowGardenUserData.setCombList(JSON.parseObject(rs.getString("combList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.snowgarden.SnowGardenCombTriggerData>>() {}));
        snowGardenUserData.setRecharge(rs.getBoolean("isRecharge"));
        snowGardenUserData.setGuarantee(rs.getInt("guarantee"));
        snowGardenUserData.setDice(JSON.parseObject(rs.getString("dice"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        snowGardenUserData.setInvitedNum(rs.getInt("invitedNum"));

        return snowGardenUserData;
    }
}
